import org.w3c.dom.*;
import org.w3c.dom.Node;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;

/**
 * Created by IntelliJ IDEA.
 * User: Rajeev
 * Date: Dec 30, 2009
 * Time: 3:52:11 PM
 * To change this template use File | Settings | File Templates.
 */
public class DOMParser {
    public static void main(String[] args) {
       DOMParser dp = new DOMParser();
        dp.buildDomTree();
        
    }

    void buildDomTree() {
        File file = new File("testdsd.xml");
           try {
             DocumentBuilder builder =
               DocumentBuilderFactory.newInstance().newDocumentBuilder();
             Document doc = builder.parse(file);
             NodeList nodes =                      doc.getChildNodes();
//           Element e = doc.getDocumentElement();
//
//
//               System.out.println(nodes.getLength());
//               System.out.println(nodes.item(0).getNodeName());//.item(0).getAttributes());
//               System.out.println(nodes.item(0).getNodeValue());//.item(0).getAttributes());
//               System.out.println(nodes.item(0).getChildNodes().item(0) instanceof Comment);//.item(0).getAttributes());
Node n = nodes.item(0);
               System.out.println(n.getNodeName());            
               System.out.println(n.getNodeValue());            
               System.out.println(n.getFirstChild().getLocalName());

               System.out.println(nodes.item(0).getChildNodes().getLength());
               n = nodes.item(1);
                              System.out.println(n.getNodeName());
                              System.out.println(n.getNodeValue());
                              System.out.println(n.getFirstChild().getLocalName());


            NodeList children = nodes.item(0).getChildNodes();
               System.out.println(children.item(0).getNodeValue());




//             for (int i = 0; i < nodes.getLength(); i++) {
//               Element element = (Element) nodes.item(i);

//               NodeList title = element.getElementsByTagName("title");
//               Element line = (Element) title.item(0);

//               System.out.println("Title: " + getCharacterDataFromElement(line));


//               NodeList url = element.getElementsByTagName("url");
//               line = (Element) url.item(0);
//               System.out.println("Url: " + getCharacterDataFromElement(line));

//             }
           }
           catch (Exception e) {
               e.printStackTrace();;
           }
    }




                                               /*
         SCHEMA	 ::=  	<dsd  ( root="PENAME" )? >
  ( RULE  |  DEFINITION  |  SCHEMA )*
</dsd>

    RULE	 ::=  	<declare> DECLARATION* </declare>
	| 	<require> BOOLEXP* </require>
	| 	<if> BOOLEXP RULE* </if>
	| 	<rule ref="PENAME"/>
	| 	UNIQUE
	| 	POINTER

DECLARATION	 ::=  	ATTRIBUTEDECL
	| 	<required> ATTRIBUTEDECL* </required>
	| 	<contents> ( REGEXP  |  NORMALIZE  |  CONTENTSDEFAULT )* </contents>

ATTRIBUTEDECL	 ::=  	<attribute ( name="PANAME" )? ( type="ATTRTYPE" )? >
  ( REGEXP  |  NORMALIZE  |  ATTRIBUTEDEFAULT )*
</attribute>

ATTRTYPE	 ::=  	string  |  qname  |  qaname

BOOLEXP	 ::=  	<and> BOOLEXP* </and>
	| 	<or> BOOLEXP* </or>
	| 	<not> BOOLEXP </not>
	| 	<imply> BOOLEXP BOOLEXP </imply>
	| 	<equiv> BOOLEXP* </equiv>
	| 	<one> BOOLEXP* </one>
	| 	<parent> BOOLEXP </parent>
	| 	<ancestor> BOOLEXP </ancestor>
	| 	<child> BOOLEXP </child>
	| 	<descendant> BOOLEXP </descendant>
	| 	<this/>
	| 	<element ( name="PENAME" )? />
	| 	<attribute ( name="PANAME" )? > REGEXP? </attribute>
	| 	<contents> REGEXP* </contents>
	| 	<boolexp ref="PENAME"/>



	REGEXP	 ::=  	<sequence> REGEXP* </sequence>
	| 	<optional> REGEXP </optional>
	| 	<complement> REGEXP </complement>
	| 	<union> REGEXP* </union>
	| 	<intersection> REGEXP* </intersection>
	| 	<minus> REGEXP REGEXP </minus>
	| 	<repeat ( ( number="NUMERAL" )?  |  ( min="NUMERAL" )? ( max="NUMERAL" )? ) > REGEXP </repeat>
	| 	<string ( value="VALUE" )? />
	| 	<char ( ( set="VALUE" )?  |  min="CHAR" max="CHAR" ) />
	| 	<stringtype ref="PENAME"/>
	| 	<contenttype ref="PENAME"/>
	| 	BOOLEXP



	DEFINITION	 ::=  	<rule id="PENAME"> RULE* </rule>
	| 	<contenttype id="PENAME"> REGEXP </contenttype>
	| 	<stringtype id="PENAME"> REGEXP </stringtype>
	| 	<boolexp id="PENAME"> BOOLEXP </boolexp>


	NORMALIZE	 ::=  	<normalize  ( whitespace="WHITESPACE" )?  ( case="CASE" )? />

WHITESPACE	 ::=  	preserve  |  compress  |  trim

CASE	 ::=  	preserve  |  upper  |  lower

ATTRIBUTEDEFAULT	 ::=  	<default value="VALUE" />

CONTENTSDEFAULT	 ::=  	<default> ANYCONTENTS </default>


UNIQUE	 ::=  	<unique  ( key="VALUE" )? >
  ( BOOLEXP FIELD* | ( <select> BOOLEXP FIELD* </select> )* )
</unique>

POINTER	 ::=  	<pointer ( key="VALUE" )? > BOOLEXP? FIELD* </pointer>

FIELD	 ::=  	<attributefield name="PANAME" ( type="ATTRTYPE" )? > BOOLEXP? </attributefield>
	| 	<chardatafield ( type="ATTRTYPE" )? > BOOLEXP? </chardatafield>

*/
}
